﻿Imports System.Data
Imports System.Data.SqlClient
Imports System.ComponentModel

Public Class MonHocDAO
    Public Sub LoadGridView(ByVal dgv As DataGridView, ByVal sql As String)
        Dim load As DataProvider = New DataProvider
        dgv.DataSource = load.Sql_ExecuteTable(sql)
    End Sub
    Public Function ThemMonHoc(ByVal mhInsert As MonHocDTO) As Boolean
        Dim Insert As DataProvider = New DataProvider
        Dim sql As String = String.Format("Insert_MonHoc")
        Dim pr As SqlParameter() = {New SqlParameter("@MaMonHoc", mhInsert.MaMonHoc),
                                    New SqlParameter("@TenMonHoc", mhInsert.TenMonHoc)}
        Dim cmd As SqlCommand = New SqlCommand(sql)
        cmd.CommandType = CommandType.StoredProcedure
        For i As Integer = 0 To pr.Count() - 1
            cmd.Parameters.Add(pr(i))
        Next
        Return Insert.SQL_ExecuteNonQuery(cmd)
    End Function
    Public Function XoaMonHoc(ByVal id As String) As Boolean
        Dim Remove As DataProvider = New DataProvider
        Dim sql As String = String.Format("Remove_MonHoc")
        Dim pr As SqlParameter = New SqlParameter("@MaMonHoc", id)
        Dim cmd As SqlCommand = New SqlCommand(sql)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add(pr)
        Return Remove.SQL_ExecuteNonQuery(cmd)
    End Function
    Public Function SuaMonHoc(ByVal mhUpdate As MonHocDTO) As Boolean
        Dim Update As DataProvider = New DataProvider
        Dim sql As String = String.Format("Update_MonHoc")
        Dim pr As SqlParameter() = {New SqlParameter("@MaMonHoc", mhUpdate.MaMonHoc),
                                    New SqlParameter("@TenMonHoc", mhUpdate.TenMonHoc)}
        Dim cmd As SqlCommand = New SqlCommand(sql)
        cmd.CommandType = CommandType.StoredProcedure
        For i As Integer = 0 To pr.Count() - 1
            cmd.Parameters.Add(pr(i))
        Next
        Return Update.SQL_ExecuteNonQuery(cmd)
    End Function
    Public Function GetList() As BindingList(Of MonHocDTO)
        Dim _ListMonHoc As New BindingList(Of MonHocDTO)
        Dim GetDT As DataProvider = New DataProvider
        Dim sql As String = "select MaMonHoc, TenMonHoc from MONHOC"
        Dim rd As SqlDataReader
        rd = GetDT.SQL_ExcuteQueryReader(sql)
        While rd.Read()
            Dim item As New MonHocDTO()
            item.MaMonHoc = rd.GetString(0)
            item.TenMonHoc = rd.GetString(1)
            _ListMonHoc.Add(item)
        End While
        Return _ListMonHoc
    End Function
End Class
